package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.GiftInfo;
import com.wude.guild.R;
import com.wude.guild.adapter.ListGiftAdapter;
import com.wude.guild.face.GetGiftFace;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 唐小妹 on 2017/11/29 0029.
 * 礼包列表
 */
@SuppressLint("HandlerLeak")
public class GiftFragment extends TitleBaseFragment implements GetGiftFace {
    private SpringView springview;
    private TextView errorText;
    private ListView giftList;
    private RelativeLayout errorLayout;

    private int limit = 1;
    private String s = "";
    List<GiftInfo> list = new ArrayList<>();
    private ListGiftAdapter listGiftAdapter;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.gift_fragment, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("GiftFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("GiftFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setHeaderTitle("礼包中心");
        initView(view);
        initdata(null);
    }

    private void initView(View view) {
        springview = findView(view, R.id.springview);
        giftList = findView(view, R.id.giftList);
        errorText = findView(view, R.id.error_text);
        errorLayout = findView(view, R.id.error_layout);

        springview.setType(SpringView.Type.FOLLOW);
        springview.setListener(new JianTing());
        springview.setHeader(new DefaultHeader(getContext()));
        springview.setFooter(new DefaultFooter(getContext()));

        listGiftAdapter = new ListGiftAdapter(getActivity(), this);
        giftList.setAdapter(listGiftAdapter);
    }

    private void initdata(String name) {
        limit = 1;
        HashMap<String, String> map = new HashMap<>();
        if (name != null) {
            map.put("game_name", name);
            map.put("version", "1");
            map.put("p", limit + "");
            map.put("token", Utils.getLoginUser().token);
            HttpCom.POST(handler, HttpCom.getActivityGiftList(), map, false);
        } else {
            map.put("p", limit + "");
            map.put("version", "1");
            map.put("token", Utils.getLoginUser().token);
            HttpCom.POST(handler, HttpCom.getActivityGiftList(), map, false);
        }
    }

    /**
     * 刚进来请求
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    List<GiftInfo> giftInfos = HttpUtils.DNSFragmentGiftList(msg.obj.toString());
                    if (giftInfos != null) {
                        list.clear();
                        springview.setVisibility(View.VISIBLE);
                        errorLayout.setVisibility(View.GONE);
                        list.addAll(giftInfos);
                        listGiftAdapter.setList(list);
                    } else {
                        springview.setVisibility(View.GONE);
                        errorLayout.setVisibility(View.VISIBLE);
                        errorText.setText("未搜索到数据");
                    }
                    break;
                case 2:
                    springview.setVisibility(View.GONE);
                    errorLayout.setVisibility(View.VISIBLE);
                    errorText.setText("网络异常");
                    break;
            }
        }
    };

    @Override
    public void getSuccess() {
        listGiftAdapter.notifyDataSetChanged();
    }

    class JianTing implements SpringView.OnFreshListener {

        @Override
        public void onRefresh() {
            if (s == null || s.equals("")) {
                initdata(null);
            } else {
                initdata(s);
            }
        }

        @Override
        public void onLoadmore() {
            limit = limit + 1;
            HashMap<String, String> map = new HashMap<>();
            map.put("p", limit + "");
            map.put("version", "1");
            if (s != null && !s.equals("")) {
                map.put("game_name", s);
            }
            map.put("token", Utils.getLoginUser().token);
            HttpCom.POST(mhandler, HttpCom.getActivityGiftList(), map, false);
        }
    }

    /**
     * 加载更多
     */
    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    List<GiftInfo> giftInfos = HttpUtils.DNSFragmentGiftList(msg.obj.toString());
                    if (giftInfos != null) {
                        list.addAll(giftInfos);
                        listGiftAdapter.setList(list);
                    } else {
                        ToastUtil.showToast("已经到底了~");
                    }
                    break;
                case 2:
                    springview.setVisibility(View.GONE);
                    errorLayout.setVisibility(View.VISIBLE);
                    errorText.setText("网络异常");
                    break;
            }
        }
    };
}
